package com.kj.xuyuan.chat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.base.Constants;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.UserInfoBean;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.SPUtils;
import com.kj.xuyuan.util.StringUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.conversation.ConversationFragment;
import io.rong.imkit.utils.RouteUtils;
import io.rong.imlib.model.Conversation;

public class ChatActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_add_friend)
    TextView tvAddFriend;
    @BindView(R.id.tv_attention)
    TextView tvAttention;
    @BindView(R.id.container)
    FrameLayout container;
    @BindView(R.id.iv_voice)
    ImageView ivVoice;
    @BindView(R.id.ed_input)
    EditText edInput;
    @BindView(R.id.iv_emoji)
    ImageView ivEmoji;
    @BindView(R.id.iv_send)
    ImageView ivSend;
    @BindView(R.id.iv_image)
    ImageView ivImage;
    @BindView(R.id.iv_call)
    ImageView ivCall;
    @BindView(R.id.iv_wx)
    ImageView ivWx;
    @BindView(R.id.iv_location)
    ImageView ivLocation;
    @BindView(R.id.iv_gift)
    ImageView ivGift;

    private String chatId;
    private int userId;

    public static void actionStar(Context context, Conversation.ConversationType conversationType,String conversationId){
        Intent intent = new Intent(context, ChatActivity.class);
        intent.putExtra(RouteUtils.CONVERSATION_TYPE,conversationType);
        intent.putExtra(RouteUtils.TARGET_ID, conversationId);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_chat;
    }

    @Override
    protected void initView() {

        chatId = getIntent().getStringExtra(RouteUtils.TARGET_ID);
        if(StringUtils.isEmpty(chatId)){
            ToastUtil.showDataErrorToast(context);
            finish();
            return;
        }

        // 添加会话界面
        ConversationFragment conversationFragment = new ConversationFragment();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.container, conversationFragment);
        transaction.commit();

        getUserInfo();
    }

    private void getUserInfo() {
        Map map = new HashMap();
        map.put("id",chatId);

        OkGoUtil.postRequest(getContext(), UrlConstants.CHAT_PAGE_USER_INFO, map, new JsonCallback<BaseModel<UserInfoBean>>() {
            @Override
            public void onSuccess(Response<BaseModel<UserInfoBean>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        userId = response.body().getData().getId();
                        if(response.body().getData().getIs_friend() == 1){
                            tvAddFriend.setVisibility(View.GONE);
                        }
                        if(response.body().getData().getIs_attention() == 1){
                            tvAttention.setVisibility(View.GONE);
                        }
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<UserInfoBean>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return -1;
    }

    @OnClick({R.id.iv_back, R.id.tv_add_friend, R.id.tv_attention, R.id.iv_voice, R.id.iv_emoji, R.id.iv_send, R.id.iv_image, R.id.iv_call, R.id.iv_wx, R.id.iv_location, R.id.iv_gift})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_add_friend:
                addFriend();
                break;
            case R.id.tv_attention:
                attentionUser();
                break;
            case R.id.iv_voice:
                break;
            case R.id.iv_emoji:
                break;
            case R.id.iv_send:
                break;
            case R.id.iv_image:
                break;
            case R.id.iv_call:
                break;
            case R.id.iv_wx:
                break;
            case R.id.iv_location:
                break;
            case R.id.iv_gift:
                break;
        }
    }

    private void attentionUser() {
        if(userId <= 0){
            return;
        }
        showDialog();
        Map map = new HashMap();
        map.put("for_user_id",chatId);
        map.put("type","1");

        OkGoUtil.postRequest(getContext(), UrlConstants.ATTENTION_USER, map, new JsonCallback<BaseModel<UserInfoBean>>() {
            @Override
            public void onSuccess(Response<BaseModel<UserInfoBean>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        tvAttention.setVisibility(View.GONE);
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<BaseModel<UserInfoBean>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
                dismissDialog();
            }
        });
    }

    private void addFriend() {
        if(userId <= 0){
            return;
        }
        showDialog();
        Map map = new HashMap();
        map.put("for_user_id",chatId);

        OkGoUtil.postRequest(getContext(), UrlConstants.ADD_FRIEND, map, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        tvAddFriend.setVisibility(View.GONE);
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
                dismissDialog();
            }
        });
    }
}